#include "ImageMerger.hxx"

ImageMerger::ImageMerger()
{
    _sourceDir[0]='\0';
    _destFileName[0]='\0';
    _fileSuffix[0]='\0';
}

bool ImageMerger::createTargetImage(bool colorTargetImage)
{
    std::vector<std::string> files;

    ListFiles(this->_sourceDir,_fileSuffix, files);

    if( files.size() <  1 )
        return false;

    int maxRowIdx = 0; 
    int maxColIdx = 0;

    char filePath[256];
    char *fileName = 0;

    char formatString[256];
    sprintf(formatString, "Tile_R%%d_C%%d.%s", _fileSuffix);

    for(int i = 0; i < files.size(); i ++)
    {
        int rowIdx = 0;
        int colIdx = 0;

        memset(filePath,0,256);

        strncpy(filePath,files.at(i).c_str(),files.at(i).size());

        fileName = strstr(filePath, "Tile");

        sscanf(fileName,formatString, &rowIdx, &colIdx);

        if( rowIdx > maxRowIdx ) maxRowIdx = rowIdx;
        if( colIdx > maxColIdx ) maxColIdx = colIdx;
    }

    //_targetImage = cv::Mat::zeros((maxRowIdx + 1) * 1024, (maxColIdx + 1) * 1024, CV_8UC3);
    if( colorTargetImage )
        _targetImage = cv::Mat((maxRowIdx + 1) * 1024, (maxColIdx + 1) * 1024, CV_8UC3, cv::Scalar(255,255,255));
    else
        _targetImage = cv::Mat((maxRowIdx + 1) * 1024, (maxColIdx + 1) * 1024, CV_8UC1, cv::Scalar(0,0,0));
        
    return _targetImage.rows > 0;
}